<template>
  <el-dialog
      :close-on-click-modal="false"
      destroy-on-close
      :show-close="false"
      width="600px"
      v-model="ANPAISTATUS"
      title="候选人安排面试"
  >
    <plan-interview-form ref="form"></plan-interview-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button size="default" @click="close(ruleFormRef)">取消</el-button>
        <el-button @click="submit(ruleFormRef)" type="primary" size="default">
          确认
        </el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<script>
// 安排面试
import {onMounted, reactive, toRefs, ref} from 'vue'
import {getListByType, getInterviewerIds} from '/@/api/oa/candidate'
import {getDictByCodes} from '/@/api/sys/dict'
import {getEnterpriseUsers} from '/@/api/sys/user'
import {getRemindTimes} from '/@/api/matter'
import {homeApi} from '/@/api/home/index'
// import {Refresh} from '@element-plus/icons-vue'
import {format} from '/@/views/home/component/lib/utils'
import PlanInterviewForm from '/@/views/matter/components/PlanInterviewForm.vue'

const homeCalendar = homeApi()
export default {
  name: 'PlanInterview',
  props: {
    ANPAISTATUS: {
      type: Boolean,
      default: true
    },
    idw: {
      type: String,
      default: ''
    },
    thisTime: {
      type: String,
      default: ''
    }
  },
  components: {
    PlanInterviewForm
  },
  emits: ['AnPaiormClose'],
  setup(props, context) {
    const form = ref(null)
    const state = reactive({
      // formData: {
      //   candidateId: null,
      //   executorIds: [], // 面试官?
      //   title: '安排面试',
      //   content: '',
      //   state: 0, // 状态 -1草稿 0未开始 1进行中 2已完成 3已退回  4已取消 5已撤回
      //   priority: 0, // 优先级 0普通 1重要
      //   actionStartTime: null, // 面试开始时间
      //   actionEndTime: null, // 面试结束时间
      //   contactUserId: null, // 联系人id
      //   contactPhone: null, // 联系人电话
      //   interviewType: null, // 面试方式
      //   remindState: 'NONE', // 提醒时间, 根据枚举类MatterRemindTimeEnum
      //   remindType: [] // 提醒方式, 多选
      // },
      // interviewTimeRange: [], // 面试时间范围
      // dateType: true,
      // dictMap: {},
      // candidateList: [],
      // relatorList: [],
      // remindTimes: []
    })
    // const ruleFormRef = ref({})
    // const formRules = reactive({
    //   candidateId: {
    //     required: true,
    //     message: '请选择候选人',
    //     trigger: 'change'
    //   },
    //   title: {
    //     required: true,
    //     message: '请输入主题',
    //     trigger: 'blur'
    //   },
    //   executorIds: {
    //     required: true,
    //     message: '请选择面试官',
    //     trigger: 'change'
    //   },
    //   actionStartTime: {
    //     required: true,
    //     message: '请选择面试开始时间',
    //     trigger: 'change'
    //   }
    // })
    // const getCandidateData = async () => {
    //   // 获取初筛通过的
    //   try {
    //     const res = await getListByType('1')
    //     state.candidateList = res.data
    //   } catch (e) {
    //     console.log(e)
    //   }
    // }
    // const getRelatorData = async () => {
    //   // 面试官和联系人共用数据
    //   const res = await getEnterpriseUsers()
    //   state.relatorList = res.data
    // }
    const dealWithTime = () => {
      const data = new Date()
      let formatDateTime
      let Y = data.getFullYear()
      let M = data.getMonth() + 1
      let D = data.getDate()
      let H = data.getHours()
      let Min = data.getMinutes()
      let S = data.getSeconds()
      let W = data.getDay()
      M = M < 10 ? '0' + M : M
      D = D < 10 ? '0' + D : D
      H = H < 10 ? '0' + H : H
      Min = Min < 10 ? '0' + Min : Min
      S = S < 10 ? '0' + S : S
      // this.nowDate = Y + "年" + M + "月" + D + "日 ";
      // this.nowWeek = "周" + W ;
      // this.nowTime = H + ":" + Min + ":" + S;
      return (formatDateTime = thisTime.value + ' ' + H + ':' + Min + ':' + S)
    }
    // const interviewTimeSelected = (selectedTime) => {
    //   if (selectedTime && selectedTime.length == 2) {
    //     let nowTime = new Date()
    //     state.formData.actionStartTime = selectedTime[0]
    //     state.formData.actionEndTime = selectedTime[1]
    //     if (nowTime > new Date(selectedTime[0])) {
    //       // 现在时间小于/等于面试开始时间就是未开始, 大于才有可能是进行中/已完成
    //       state.formData.state = nowTime < selectedTime[1] ? 1 : 2
    //     }
    //   } else {
    //     state.formData.actionStartTime = state.formData.actionEndTime = null
    //   }
    // }
    // const getRemindData = async () => {
    //   try {
    //     const res = await getRemindTimes()
    //     state.remindTimes = res.data
    //   } catch (e) {
    //     console.log(e)
    //   }
    // }
    // const candidateChange = (val) => {
    //   if (val) {
    //     handleDefaultInterviewers()
    //   }
    // }
    // const resetForm = (formEl) => {
    //   if (!formEl) return
    //   formEl.resetFields()
    // }
    // const handleDefaultInterviewers = async () => {
    //   try {
    //     const res = await getInterviewerIds(state.formData.candidateId)
    //     if (res.data && res.data.length > 0) {
    //       state.formData.executorIds = Object.assign([], res.data)
    //     }
    //   } catch (e) {
    //     console.log(e)
    //   }
    // }
    // const contactChange = async (val) => {
    //   // 选择联系人后自动带入手机号
    //   for (let i = 0; i < state.relatorList.length; i++) {
    //     if (state.relatorList[i].userId == val) {
    //       state.formData.contactPhone = state.relatorList[i].phone
    //       break
    //     }
    //   }
    // }
    // let close = (formEl, data) => {
    //   if (data) {
    //     context.emit('AnPaiormClose', data)
    //   } else {
    //     context.emit('AnPaiormClose')
    //   }
    //   // resetForm(formEl)
    //   form.value.reset()
    // }
    let submit = async () => {
      form.value.submit()
      // state.formData.occurDate = format(state.occurDate, 'yyyy-MM-dd HH:mm')
      // state.formData.settlementDate = format(state.settlementDate, 'yyyy-MM-dd HH:mm')
      // state.form.id = idw.value;
      // if (!formEl) return
      // await formEl.validate((valid, fields) => {
      //   if (valid) {
      //     console.log(state.formData)
      //     let str = state.formData.remindType.join(',')
      //     state.formData.remindType = str
      //     // state.formData.executorIds = JSON.stringify(state.formData.executorIds)
      //     homeCalendar.addPlanInterview(state.formData).then((res) => {
      //       if (res.code === 0) {
      //         resetForm(formEl)
      //         close(formEl, res.data)
      //       }
      //     })
      //   } else {
      //     console.log('error submit!', fields)
      //   }
      // })

      // console.log(state.formData);
      // state.form.id = idw.value
      // homeCalendar
      //   .modifyMatter(state.formData)
      //   .then((res) => {
      //       if(res.code===1){

      //       }
      //   });
    }
    onMounted(() => {
      // getDictByCodes(['InterviewType']).then((res) => (state.dictMap = res.data))
      // getCandidateData()
      // getRelatorData()
      // getRemindData()
      // // 如果带有候选人, 自动获取该候选人的面试官列表
      // if (state.formData.candidateId) {
      //   handleDefaultInterviewers()
      // }
    })
    return {
      ...toRefs(state),
      // Refresh,
      // getCandidateData,
      // interviewTimeSelected,
      // getRelatorData,
      // getRemindData,
      // handleDefaultInterviewers,
      // candidateChange,
      // contactChange,
      // ruleFormRef,
      submit,
      close,
      // resetForm,
      // formRules,
      form
    }
  }
}
</script>

<style lang="scss" scoped></style>
